package com.zw.myapplication

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Parcelable
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.ltt.mylibrary.activity.AllFileActivity
import com.ltt.mylibrary.activity.LocalFileActivity
import com.ltt.mylibrary.utils.FileConfig
import com.ltt.mylibrary.utils.PermissionCheckUtils
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.Permission


class MainActivity : AppCompatActivity() {
    private val mBtnStart: Button by lazy { findViewById<Button>(R.id.btn_start) }
    private val mActivityMain: LinearLayout by lazy { findViewById<LinearLayout>(R.id.activity_main) }
    private val mTvResult: TextView by lazy { findViewById<TextView>(R.id.tv_result) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        PermissionCheckUtils.setOnOnWantToOpenPermissionListener {
            Toast.makeText(
                this@MainActivity,
                "请去设置的应用管理中打开应用读取内存的权限",
                Toast.LENGTH_SHORT
            ).show()
        }
        mBtnStart.setOnClickListener {
            jumpActivity()
        }
        initData()

    }

    private fun jumpActivity(){
//        val intent = Intent(this, LocalFileActivity::class.java)
//        startActivityForResult(intent, 200)
//        overridePendingTransition(R.anim.enter, R.anim.exit);
        FileConfig.Builder(this).setCount(5).setRequestCode(200).startActivityAllForResult()
    }
    private fun initData() {
        AndPermission.with(this)
            .runtime()
            .permission(Permission.Group.STORAGE)
            .onGranted {
            }
            .onDenied {  }
            .start()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (requestCode == 200) {
            if (resultCode == 200) {
                val resultFileList= data.getParcelableArrayListExtra<Parcelable>("file")
                if (resultFileList != null && resultFileList.size > 0) {
                    mTvResult.text = resultFileList.toString()
                }
            }
        }
    }

}